<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>_${customer.customerName}_上架</title>
<style type="text/css">
	.chosen-container {
		width: 100%!important;
	}
</style>
<script type="text/javascript" src="${ctx}/static/v2/js/modal.js"></script>
<script type="text/javascript">
    var warehouseCode;
    var customerCode;
    var billCode;
    var skuLockValue="";
	$(function() {
		$("#txt_lpNum").val("");
		$("#txt_lpNum").focus();
		$("#txt_sku_1").val("");
		$("#txt_num").val("1");
		$("#txt_upStorage").val("");
		//$("#recommendStorage").val("");
		$("#msg_1").html("");
		//锁定数量
		$("#lock_num").iCheck({checkboxClass: 'icheckbox_minimal'});
		$("#lock_num").parent().attr('aria-checked','true');
		
        $('#lock_num').on('ifChecked', function (event) {
			$("#txt_num").attr("readOnly", true);
        });
        $('#lock_num').on('ifUnchecked', function (event) {
        	$("#txt_num").removeAttr("readOnly");
        	$("#txt_num").val('');
        });
		warehouseCode="${task.inwarehouseCode}";
		customerCode="${task.customerCode}";
		billCode="${task.billCode}"
	});

	function lpnChange(){
		var lpn=$("#txt_lpNum").val().trim();
		 $.ajax({
			   type : 'post',
				url : '${ctx}/v2_instorageBillNew/getUnUpAndUpDetailByLPN',
				async:false,
				data : {
					lpn:lpn,
					billCode:billCode
				},
				dataType : 'json',
				success : function(data) {
					var status=data.status;
					var content = data.content;
				    if( status=='success'){
					       var unUpitems=data.unUpitems;
					       var rowNum = 1 ;
					       // by zhangwieze 增加对lpn对应的未上架数据为0的判断提示 L58-L61
					       if(data.unUpitems.length==0){
					    	   jAlert("该LPN没有未上架数据,请核对!","错误提示");
					    	   $("#txt_lpNum").val('');
					       }
					       $("#unUpItem").html('');
					       var str='';
					       $.each(unUpitems,function(i,item){
					    	   if(item.upNum == item.countNum){
					    		   str+="<tr id='tr_"+item.id+"' style='background:#FC554F;'>";
					    	   }else{
					    		   str+="<tr id='tr_"+item.id+"' >";
					    	   }
					    	   str+="<td>"+rowNum+"</td>";
					    	   rowNum++ ;
					    	   str+="<td>"+item.billCode+"</td>";
					    	   if(item.errorFlag=='noterror'){
					    	 	  str+="<td>完好件</td>";
					    	   }else if(item.errorFlag=='error'){
					    	 	  str+="<td>破损件</td>";
					    	   }
					    	   str+="<td>"+item.lpnNo+"</td>";
					    	   str+="<td>"+item.sku+"</td>";
					    	   str+="<td>"+item.batchCode+"</td>";
					    	   str+="<td>"+item.productDate+"</td>";
					    	   str+="<td>"+item.expireDate+"</td>";
					    	   str+="<td>"+item.countNum+"</td>";
					    	   if(item.status=='0'){
						    	 	 str+="<td>未上架</td>";
						    	}else if(item.status=='1'){
						    	 	 str+="<td>部分上架</td>";
						    	}else if(item.status=='2'){
						    	 	 str+="<td>完全上架</td>";
						    	}
					    	   str+="<td>"+item.unUpNum+"</td>";
					    	   if(item.unUpNum>0){
						    	   str+="<td><a class=\"btn btn-primary\" onclick=\"cancalReceipt("+item.id+");\">取消收货</a></td>";
					    	   }else{
					    		   str+="<td>完成</td>";
					    	   }
					    	   
					    	   str+="</tr>";
					       });
					       $("#unUpItem").html(str);  
					       
					       $("#upItem").html('');
					       var upitems=data.upitems;
					       rowNum = 1 ;
					       var str='';
					       $.each(upitems,function(i,item){
					    	   str+="<tr id='tr_"+item.id+"' >";
					    	   str+="<td>"+rowNum+"</td>";
					    	   rowNum++ ;
					    	   /**
					    	   if(item.errorFlag=='noterror'){
						          str+="<td>完好件</td>";
					    	   }else if(item.errorFlag=='error'){
					    	 	  str+="<td>破损件</td>";
					    	   }
					    	   **/
					    	   str+="<td>"+item.sku+"</td>";
					    	   str+="<td>"+item.batchCode+"</td>";
					    	   str+="<td>"+item.productDate+"</td>";
					    	   str+="<td>"+item.expireDate+"</td>";
					    	   str+="<td>"+item.upNum+"</td>";
					    	   str+="<td>"+item.stockCode+"</td>"
					    	   str+="</tr>";
					       });
					       $("#upItem").html(str); 
					       $('#txt_sku_1').focus();
				    }else{
				    	 alert(content);
				    }
				},
				error : function() {
					jAlert("网络繁忙，请稍后重试...","温馨提示");
				}
			}); 
	}
	//by zhangwieze 区别完成上架与LPNchange事件弹出的信息 L130,L213
	function upLpnChange(){
		var lpn=$("#txt_lpNum").val().trim();
		 $.ajax({
			   type : 'post',
				url : '${ctx}/v2_instorageBillNew/getUnUpAndUpDetailByLPN',
				async:false,
				data : {
					lpn:lpn,
					billCode:billCode
				},
				dataType : 'json',
				success : function(data) {
					var status=data.status;
					var content = data.content;
				    if( status=='success'){
					       var unUpitems=data.unUpitems;
					       var rowNum = 1 ;
					       $("#unUpItem").html('');
					       var str='';
					       $.each(unUpitems,function(i,item){
					    	   if(item.upNum == item.countNum){
					    		   str+="<tr id='tr_"+item.id+"' style='background:#FC554F;'>";
					    	   }else{
					    		   str+="<tr id='tr_"+item.id+"' >";
					    	   }
					    	   str+="<td>"+rowNum+"</td>";
					    	   rowNum++ ;
					    	   str+="<td>"+item.billCode+"</td>";
					    	   if(item.errorFlag=='noterror'){
					    	 	  str+="<td>完好件</td>";
					    	   }else if(item.errorFlag=='error'){
					    	 	  str+="<td>破损件</td>";
					    	   }
					    	   str+="<td>"+item.lpnNo+"</td>";
					    	   str+="<td>"+item.sku+"</td>";
					    	   str+="<td>"+item.batchCode+"</td>";
					    	   str+="<td>"+item.productDate+"</td>";
					    	   str+="<td>"+item.expireDate+"</td>";
					    	   str+="<td>"+item.countNum+"</td>";
					    	   if(item.status=='0'){
						    	 	 str+="<td>未上架</td>";
						    	}else if(item.status=='1'){
						    	 	 str+="<td>部分上架</td>";
						    	}else if(item.status=='2'){
						    	 	 str+="<td>完全上架</td>";
						    	}
					    	   str+="<td>"+item.unUpNum+"</td>";
					    	   if(item.unUpNum>0){
						    	   str+="<td><a class=\"btn btn-primary\" onclick=\"cancalReceipt("+item.id+");\">取消收货</a></td>";
					    	   }else{
					    		   str+="<td>完成</td>";
					    	   }
					    	   
					    	   str+="</tr>";
					       });
					       $("#unUpItem").html(str);  
					       
					       $("#upItem").html('');
					       var upitems=data.upitems;
					       rowNum = 1 ;
					       var str='';
					       $.each(upitems,function(i,item){
					    	   str+="<tr id='tr_"+item.id+"' >";
					    	   str+="<td>"+rowNum+"</td>";
					    	   rowNum++ ;
					    	   /**
					    	   if(item.errorFlag=='noterror'){
						          str+="<td>完好件</td>";
					    	   }else if(item.errorFlag=='error'){
					    	 	  str+="<td>破损件</td>";
					    	   }
					    	   **/
					    	   str+="<td>"+item.sku+"</td>";
					    	   str+="<td>"+item.batchCode+"</td>";
					    	   str+="<td>"+item.productDate+"</td>";
					    	   str+="<td>"+item.expireDate+"</td>";
					    	   str+="<td>"+item.upNum+"</td>";
					    	   str+="<td>"+item.stockCode+"</td>"
					    	   str+="</tr>";
					       });
					       $("#upItem").html(str); 
				    }else{
				    	 alert(content);
				    }
				},
				error : function() {
					jAlert("网络繁忙，请稍后重试...","温馨提示");
				}
			}); 
	}
	function cancalReceipt(id){
		 $.ajax({
			   type : 'post',
				url : '${ctx}/v2_instorageBillNew/cancalReceipt',
				async:false,
				data : {
					id:id
				},
				dataType : 'json',
				success : function(data) {
					var status=data.status;
					var content = data.content;
				    if( status=='success'){
				    	upLpnChange();
				    }else{
				    	 alert(content);
				    }
				},
				error : function() {
					jAlert("网络繁忙，请稍后重试...","温馨提示");
				}
			}); 
	}

	function numChange(){
		// $("#txt_upStorage").val('');
		$("#txt_upStorage").focus();
	}
	function errorChange() {		
		$("#txt_sku_1").val("");
		$("#txt_sku_1").focus();
		var lock_num = $("#lock_num").parent().attr('aria-checked');
    	if (!lock_num) {
			$("#txt_num").val("");
    	}
		$("#txt_upStorage").val("");
		$("#msg_1").html("");
	}
	function upStorageChange(){
		
			$("#msg_1").html("");
			var upStorage=$("#txt_upStorage").val().trim();
			 $.ajax({
				   type : 'post',
					url : '${ctx}/v2_instorageBillNew/checkUpStorage',
					async:false,
					data : {
						upStorage:upStorage
					},
					dataType : 'json',
					success : function(data) {
						var status=data.status;
						var content = data.content;
						var rowNum = 1 ;
					    if( status=='success'){
					    	$("#msg_1").html("库位可以使用!");
					    }else{
					    	 $("#msg_1").html(content);
					    	 $("#txt_upStorage").val("");
					    }
					},
					error : function() {
						jAlert("网络繁忙，请稍后重试...","温馨提示");
					}
				}); 
	 
	}
	function recommendStorageClick(){
		var options=$("#recommendStorage option:selected");
	}
	function up(){
		var lpn=$("#txt_lpNum").val().trim();
		var sku=$("#txt_sku_1").val().trim();
		var num=$("#txt_num").val().trim();
		var batchCode = $("#txt_batchCode").val().trim();
		var productDate = $("#txt_productDate").val().trim();
		var expireDate = $("#txt_expireDate").val().trim();
		var upStorage=$("#txt_upStorage").val().trim();
		var errorFlag=$("#error_flag").val().trim();
		if(sku.length==0){
			alert("SKU不能为空!");
			$("#txt_sku_1").focus();
			return false;
		}
		if(num.length==0){
			alert("数量不能为空!");
			$("#txt_num").focus();
			return false;
		}
		if(upStorage.length==0){
			alert("上架库位不能为空!");
			$("#txt_upStorage").focus();
			return false;
		}
		 $.ajax({
			   type : 'post',
				url : '${ctx}/v2_instorageBillNew/upShelf',
				async:false,
				dataType:'json',
				data : {
					upStorage:upStorage,
					lpn:lpn,
					sku:sku,
					batchCode:batchCode,
					productDate:productDate,
					expireDate:expireDate,
					num:num,
					billCode:billCode,
					errorFlag:errorFlag
				},
				dataType : 'json',
				success : function(data) {
						msg=data.msg;
				    	if(msg=='finishing'){
				    		jAlert("上架成功.");
					    }
				    	//by zhangwieze 修改弹出信息 L250,L253
					    else if(msg=='uped'){
					        jAlert("该订单已上架！");
					    }
					    else if(msg=='finish'){
					    	jAlert("sku错误！");
					    	$('#txt_sku_1').focus();
					    }
					    else if(msg=='S009'){
					        jAlert("入库单已取消");
				       }else if(msg=='numOver'){
					        jAlert("上架数量和待数量不一致.请核对");
					    }else if(msg=='isEmpty'){
					    	jAlert("入库单已关闭");
					    }else if(msg=="sotrageExist"){
					    	jAlert("库位存在");
					    }else if(msg == "notMatch"){
					    	jAlert("库位sku、批次号、生产日期、过期日期不一致");
					    }else{
					    	jAlert("上架失败");
					    }
				    	$("#txt_sku_1").val("");
				    	$("#txt_batchCode").val("");
				    	$("#txt_productDate").val("");
				    	$("#txt_expireDate").val("");
			        	$("#txt_sku_1").focus();
			        	var lock_num = $("#lock_num").parent().attr('aria-checked');
			        	if (!(lock_num == 'true')) {
			    			$("#txt_num").val("");
			        	}
			    		$("#txt_upStorage").val("");
			    		batchTime = 0;
			    		upLpnChange();
				},
				error : function() {
					jAlert("网络繁忙，请稍后重试...","温馨提示");
				}
			}); 
	}
	function clearBatch() {
		$('#txt_batchCode').val('');
		$('#txt_productDate').val('');
		$('#txt_expireDate').val('');
	  	$('.txt_batchCode').css('display','none');
	  	$('.txt_expireDate').css('display','none');
	 }
	// SKU的enter事件
	var batchTime = 0;
	function keydown_key() {
		var lock_num = $("#lock_num").parent().attr('aria-checked');
		keydown_1();
	}
	function keydown_1(){
		var lock_num = $("#lock_num").parent().attr('aria-checked');
	  	customerCode="${task.customerCode}";
	  	billCode="${task.billCode}";
	  	var lpnNo = $("#txt_lpNum").val().trim();
	  	if (lpnNo.length <= 0) {
	  		jAlert("请先输入lpn号");
	  		return;
	  	}
	  	var sku = $("#txt_sku_1").val().trim(); 
	  	$.ajax({
			type:'post',
			url:'${ctx}/v2_instorageBillNew/findSku',
			data:{
				lpnNo:lpnNo,
				billCode:billCode,
				customerCode:customerCode,
				sku:sku
			},
			dataType:'json',
			success:function(data) {
				if(data.status == "success"){
					batchTime = 1;
					if(data.isShelfLifeMgmt != 0 || data.isBatchMgmt != 0){
						var batchList, batchCode_modal, productDate_modal, expireDate_modal, model_data;
						batch(data.isShelfLifeMgmt, data.isBatchMgmt, data.batchList ,function(batchList){
							batchCode_modal = batchList[0].batch_input == undefined ? '' : batchList[0].batch_input;
							productDate_modal = batchList[1].batch_start == undefined ? '' : batchList[1].batch_start;
							expireDate_modal = batchList[2].batch_end == undefined ? '' : batchList[2].batch_end;
							if (data.isBatchMgmt != 0) {
								$("#txt_batchCode").val(batchCode_modal);
								$('.txt_batchCode').css('display','inline-block');
							}
							if (data.isShelfLifeMgmt != 0) {
								$("#txt_productDate").val(productDate_modal);
								$("#txt_expireDate").val(expireDate_modal);
								$('.txt_expireDate').css('display','inline-block');
							}
					  	  	if(lock_num == 'true'){
					  		  	numChange();
					  	  	}else{
					  		  	$("#txt_num").focus();
					  	  	}
						});
					}else{
						batchCode = '';
						productDate = '';
						expireDate = '';
						if(lock_num == 'true'){
				  		  	numChange();
				  	  	}else{
				  		  	$("#txt_num").focus();
				  	  	}
					} 	
			    }else if(data.status == "skuNotExist"){
			       alert(data.content);
			       $("#txt_sku_1").val('');
			       $("#txt_sku_1").focus();
			       if(!(lock_num == 'true')){
			       		$('#txt_num').val('');
			       }
			    }else {
			      alert("网络繁忙，请稍后重试...");
			    }
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	   	}); 
	}

	function checkBatch() {
		if (batchTime == 0) {
			keydown_1();
		}
	}
</script>
</head>

<body>
	<section class="content">

		<table id="contentTable" class="table  table-bordered table-condensed">
			<thead>
				<tr>
				<th style="text-align: center; color: white;"
					bgcolor="#3c8dbc">上架</th>
				</tr>
			</thead>
			<tr>
				<td>
				<span style="margin-left: 10px;">是否报损件:</span> 
				<select
					id="error_flag" class="oldSelectStyle " onchange="errorChange();">
						<option value="noterror">完好件</option>
						<option value="error">破损件</option>
				</select> 
				<span style="margin-left: 10px;">LPN(跟踪号):</span> 
					<input id="txt_lpNum" type="text"  onchange="lpnChange();"  /> 
					<span style="margin-left: 12px; margin-top: 10px; color: red; font-weight: bold;" id="msg_1"></span></td>
				</td>
			</tr>
			<tr>
				<td>
				<span style="margin-left: 10px;">SKU:</span> 
				<input id="txt_sku_1" type="text" onkeydown="if(event.keyCode == '13'){keydown_key();}" onchange="clearBatch()"/> 
				<div style="display:none;" class="txt_batchCode">
					<span style="margin-left: 10px;">批次号:</span> 
					<input id="txt_batchCode" type="text" disabled/> 
				</div>
				<div style="display:none;" class="txt_expireDate">
					<span style="margin-left: 10px;">生产日期:</span> 
					<input id="txt_productDate" type="text" disabled/> 
				</div>
				<div style="display:none;" class="txt_expireDate">
					<span style="margin-left: 10px;">过期日期:</span> 
					<input id="txt_expireDate" type="text" disabled/> 
				</div>
				
				<span style="margin-left: 10px;">数量:</span> 
				<input id="txt_num" type="text" onkeydown="if(event.keyCode == '13'){numChange();}" oninput="checkBatch()"/> 
				<input id="lock_num" name="lock_num" type="checkbox"  checked="checked">锁定数量</input>
				<span style="margin-left: 10px;">上架库位:</span> 
				<input id="txt_upStorage" type="text"  oninput="checkBatch()"/> 
				<!-- 
				<span style="margin-left: 10px;">推荐库位:</span> 
				<select  name="recommendStorage"  id="recommendStorage"   class="oldSelectStyle" style="width:120px;" onclick="recommendStorageClick();" >
				<option value=""></option>
				</select>
				 -->
				<a class="btn btn-primary" onclick="up();">上架</a>
				<a class="btn btn-primary" href="${ctx}/v2_instorageBillNew/v2_toInstorageBillListPage">返回</a>
				</td>
			</tr>
		</table>

		<!-- 上架数据 -->
		<table class="table" style="margin-top: 0px;">
			<thead>
				<tr><th style="text-align:center;color:white" colspan="8"
					bgcolor="#3c8dbc">上架数据</th></tr>
				<tr>
					<th style="width: 50px;">序号</th>
					<!--  <th style="width: 150px;">报损状态</th>-->
					<th style="width: 150px;">SKU</th>
					<th style="width: 100px;">批次号</th>
					<th style="width: 100px;">生产日期</th>
					<th style="width: 100px;">过期日期</th>
					<th style="width: 50px;">数量</th>
					<th style="width: 100px;">库位编码</th>
				</tr>
			</thead>
			<tbody id="upItem">
				<c:forEach items="${upItems}" var="t" varStatus="i">
					<tr>
						<td >${i.count }</td>
						<!--
						<td >
							<c:if test="${t.errorFlag eq 'noterror'}">
								完好件
							</c:if>
							<c:if test="${t.errorFlag eq 'error'}">
								破损件
							</c:if>
						</td>
						  -->
						<td >${t.sku}</td>
						<td >${t.batchCode}</td>
						<td >${t.productDate}</td>
						<td >${t.expireDate}</td>
						<td >0</td>
						<td >0</td>
					</tr>
				</c:forEach>
			</tbody>

		</table>
		
		<!-- 待上架数据 -->
		<table class="table" style="margin-top: 0px;">
			<thead>
				<tr><th style="text-align:center;color:white" colspan="12"
					bgcolor="#3c8dbc">待上架数据</th></tr>
				<tr>
					<th style="width: 50px;">序号</th>
					<th style="width: 150px;">入库单号</th>
					<th style="width: 150px;">报损状态</th>
					<th style="width: 50px;">LPN</th>
					<th style="width: 150px;">SKU</th>
					<th style="width: 150px;">批次号</th>
					<th style="width: 150px;">生产日期</th>
					<th style="width: 150px;">过期日期</th>
					<th style="width: 50px;">收货数量</th>
					<th style="width: 50px;">上架状态</th>
					<th style="width: 50px;">待上架数量</th>
					<th style="width: 50px;">操作</th>
				</tr>
			</thead>
			<tbody id="unUpItem">
				
			</tbody>

		</table>
	</section>
</body>
</html>
